#include("/admin/common/header.html")
#include("/admin/common/footer.html")
#include("/admin/common/left.html")
#include("/admin/common/layout.html")
 
 #@layout()
 #define content()

<span class="layui-breadcrumb">
  <a href="#(ctx_path)/">首页</a>
  <a href="#(ctx_path)/admin/shop/order"><cite>订单管理</cite></a>
</span>

<hr>


<div class="layui-field-box">
<form class="layui-form" id="searchForm">
    <div class="layui-form-item">
    		<div class="layui-inline">
	      <label class="layui-form-label">搜索内容</label>
	      <div class="layui-input-inline">
	        <input type="text" name="searchContent" autocomplete="off" class="layui-input" placeholder="请输入内容搜索">
	      </div>
	    </div>
	    
	    <div class="layui-inline">
	      <label class="layui-form-label">状态</label>
	      <div class="layui-input-inline">
	      		#set(DIC_MAP={})
	      		#set(DIC_MAP["dicTypeCode"]="SHOP_ORDER_STATUS")
	      		#set(DIC_MAP["name"]="status")
	      	   	#set(DIC_MAP["id"]="status")
	        	#@dic_get_select(DIC_MAP)
	      </div>
	    </div>
	    
	    <button class="layui-btn" type="button" id="btnSearch">搜索</button>
	    <button class="layui-btn layui-btn-normal" type="reset" >重置</button>
	    
	</div>
</form>	    
</div>
  
<table class="layui-hide" id="dataTable"></table>


<script type="text/html" id="actionTpl">
	<a href="javascript:doDetail('{{d.id}}');" class="layui-table-link" actionType="detail" actionId="{{d.id}}">详情</a>
	{{#
		if(d.status=='2'){
	}}
	<a href="javascript:doVerify('{{d.id}}');" class="layui-table-link" actionType="detail" actionId="{{d.id}}">确认</a>	
	{{#
		}else if(d.status=='3'){
	}}
	<a href="javascript:doDelivery('{{d.id}}');" class="layui-table-link" actionType="detail" actionId="{{d.id}}">发货</a>
	{{#
		}
	}}

	{{#
		if(d.status!='6'&&d.status!='100'){
	}}
	<!--订单没有完成，可以取消订单-->
	<a href="javascript:doCancel('{{d.id}}');" class="layui-table-link" actionType="detail" actionId="{{d.id}}">取消</a>
	{{#
		}

	}}

	{{#
		if(d.payStatus!='1'&&d.status!='100'){
	}}
	<!--已经支付的订单，并且订单没有完成，可以退款-->
	<a href="javascript:doRefund('{{d.id}}');" class="layui-table-link" actionType="detail" actionId="{{d.id}}">退款</a>
	{{#
		}

	}}

	{{#
		if(d.status=='4'){
	}}
	<!--已经发货的订单可以确认完成-->
	<a href="javascript:doComplete('{{d.id}}');" class="layui-table-link" actionType="detail" actionId="{{d.id}}">完成</a>
	{{#
		}

	}}

</script>

#end

<div id="delivery_model" class="layui-form" style="width: 80%;display: none;" align="center">

	<div class="layui-form-item" style="margin-top:10px;" >
		<label class="layui-form-label">快递</label>
		<div class="layui-input-block">
			#set(DIC_MAP={})
			#set(DIC_MAP["dicTypeCode"]="COURIER")
			#set(DIC_MAP["nullValueName"]="请选择快递")
			#set(DIC_MAP["name"]="delivery_logisticsType")
			#set(DIC_MAP["id"]="delivery_logisticsType")
			#@dic_get_select(DIC_MAP)
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">快递编号</label>
		<div class="layui-input-block">
			<input type="text" name="delivery_logisticsNumber" required lay-verify="required" placeholder="请输入快递编号" autocomplete="off" class="layui-input">
		</div>
	</div>

</div>

#define js()

<script>


	layui.use('table', function() {
		
		var table = layui.table;
		table.render({
			elem : '#dataTable',
			url : '#(ctx_path)/admin/shop/order/pagequery',
			cols : [ [
			  {field:'number', title: '订单编号',width:'15%'},
		      {field:'amount', title: '订单价格',width:'9%'},
			  {field:'status_dic', title: '订单状态',width:'9%'},
			  {field:'createTime', title: '提交时间',width:'20%'},
		      {templet:"#actionTpl",title: '操作'}
			] ],
			page : true
		});
		
		$("#btnSearch").click(function(){
			
			var searchContent=$(":input[name='searchContent']").val();
			var status=$(":input[name='status']").val();
			
			//执行重载
			table.reload('dataTable', {
				page : {
					curr : 1//重新从第 1 页开始
				},
				where : {
					searchContent:searchContent,
					status:status
				}
			});
			
		});

	});

	function doDetail(id) {
		window.location.href = "#(ctx_path)/admin/shop/order/view?id=" + id;
	}

	function doVerify(id){
		layui.layer.confirm('确认订单么?', function(index) {
			$.ajax({
				type : 'get',
				url : '#(ctx_path)/admin/shop/order/verify',
				data : 'id=' + id,
				success : function(rep) {
					if(rep.code=="99"){
						layer.msg('测量信息未完善，请先填写测量信息！', {
							time : 1000*5
						}, function() {
							
						});
					}else{
						layer.msg('确认成功！', {
							time : 600
						}, function() {
							
						});
						layui.table.reload('dataTable');
					}
				}
			});
			layui.layer.close(index);
		});
	}

	function doDelivery(id){

		layui.layer.open({
			type: 1,
			content:$("#delivery_model"),
			area: ['420px', '400px'], //宽高
			btn: ['确认', '取消'],
			yes: function(index, layero){

				var logisticsType=$(":input[name='delivery_logisticsType']").val();
				var logisticsNumber=$(":input[name='delivery_logisticsNumber']").val();

				if(logisticsType==""){
					alert("请选择快递");
					return;
				}
				if(logisticsNumber==""){
					alert("请输入快递编号");
					return;
				}

				$.ajax({
					type : 'get',
					url : '#(ctx_path)/admin/shop/order/delivery',
					data : 'id='+id+"&logisticsType="+logisticsType+"&logisticsNumber="+logisticsNumber,
					success : function(rep) {
						if(rep.code=="0"){
							layer.msg('发货成功', {
								time : 600
							}, function() {
								
							});
							layui.table.reload('dataTable');
						}
					}
				});
				layui.layer.close(index);

			},
			btn2: function(index, layero){
				layui.layer.close(index);
			}
			
		});
		
	}

	function doCancel(id){
		layui.layer.confirm('确认取消订单么?', function(index) {
			$.ajax({
				type : 'get',
				url : '#(ctx_path)/admin/shop/order/cancel',
				data : 'id=' + id,
				success : function(rep) {
					if(rep.code=="0"){
						layer.msg('订单取消成功', {
							time : 600
						}, function() {
							
						});
						layui.table.reload('dataTable');
					}
				}
			});
			layui.layer.close(index);
		});
	}

	function doRefund(id){
		//退款操作因为涉及到接口，时间比较慢，所以使用加载框
		layui.layer.confirm('确认退款么?', function(index) {

			var loadindex=layui.layer.load(1, {
				shade: [0.6],
			});

			$.ajax({
				type : 'get',
				url : '#(ctx_path)/admin/shop/order/refund',
				data : 'id=' + id,
				success : function(rep) {

					layui.layer.close(loadindex);

					console.log(rep);

					var obj=rep.data;
					var msg=rep.msg;
					if(rep.code=="0"){
						if(obj.payType=='pay_alipay'){
							//支付宝,支付宝退款需要跳转到支付宝网站上来退款
							window.location.href="#(ctx_path)/alipay/refund?orderId="+id;
						}
					}else{
						layer.msg(msg, {
							time : 3000
						}, function() {
							
						});
					}

					//微信可以后台退款,所以直接刷新
					layui.table.reload('dataTable');
				}
			});
			layui.layer.close(index);
		});
	}

	function doComplete(id){
		//退款操作因为涉及到接口，时间比较慢，所以使用加载框
		layui.layer.confirm('确认完成订单么?', function(index) {

			$.ajax({
				type : 'get',
				url : '#(ctx_path)/admin/shop/order/complete',
				data : 'id=' + id,
				success : function(rep) {

					var obj=rep.data;
					var msg=rep.msg;
					layui.table.reload('dataTable');
				}
			});
			layui.layer.close(index);
		});
	}


</script>

#end


